﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yu.Cms.Common;
using System.Xml;

namespace Yu.Cms.Web.UserCtrl.Admin
{
    public partial class Link : BaseCtrl
    {
        //private string configPath = Config.FriendLinkFileManager.ConfigFilePath;
        private string configPath = Utils.GetMapPath(Yu.Cms.Config.BaseConfigs.GetSitePath + "config/aggregation.config");
        private string nodeName = "Link";
        private string targetNode = "FriendLinkInfo";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.OnStar();
            }
        }
        protected void OnStar()
        {
            XmlLinkSource.DataFile = configPath;
            this.BindLink();
        }

        protected void BindLink()
        {
            RptPic.DataSource = XmlLinkSource;
            RptPic.DataBind();
        }


        /// <summary>
        /// 添加链接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if ((!Utils.IsURL(txtUrl.Text.Trim())))
            {
                JScript.AlertAndGoHistory("Logo路径或点击链接可能是非法URL");
                if (drpType.SelectedValue == "1" && !Utils.IsURL(txtLogo.Text.Trim()))
                {
                    JScript.AlertAndGoHistory("Logo路径或点击链接可能是非法URL");
                    return;
                }
                return;               
            }
            XmlDocumentExtender doc = new XmlDocumentExtender();
            doc.Load(configPath);
            int lastLinkID = 0;
            if (doc.SelectSingleNode(targetNode) != null)
            {
                if (doc.SelectSingleNode(targetNode).InnerText != "")
                {
                    lastLinkID = int.Parse(doc.SelectSingleNode(targetNode).LastChild["LinkID"].InnerText);
                }
            }

            lastLinkID++;

            

            XmlElement FriendLinkNode = doc.CreateElement(nodeName);
           
            doc.AppendChildElementByNameValue(ref FriendLinkNode, "LinkID", lastLinkID.ToString());
            doc.AppendChildElementByNameValue(ref FriendLinkNode, "Name", txtName.Text.Trim());            
            doc.AppendChildElementByNameValue(ref FriendLinkNode, "Url", txtUrl.Text.Trim());
            doc.AppendChildElementByNameValue(ref FriendLinkNode, "LinkType", drpType.SelectedValue);
            doc.AppendChildElementByNameValue(ref FriendLinkNode, "Logo", txtLogo.Text.Trim());
            doc.AppendChildElementByNameValue(ref FriendLinkNode, "Info", txtInfo.Text.Trim());
            doc.AppendChildElementByNameValue(ref FriendLinkNode, "PubDate", DateTime.Now);

            doc.CreateNode(targetNode).AppendChild(FriendLinkNode);
            doc.Save(configPath);
            //BindLink(); ;
            JScript.AlertAndRedirect("添加成功", "RotatePic.aspx");
        }

        protected void btnModify_Click(object sender, EventArgs e)
        {

        }

        protected void btnDel_Click(object sender, EventArgs e)
        {

        }

        protected void RptPic_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                HiddenField hdType = (HiddenField)e.Item.FindControl("hdType");
                DropDownList drpLink = (DropDownList)e.Item.FindControl("drpLink");
                if (hdType.Value == "0")
                {
                    drpLink.Items.FindByValue("0").Selected = true;
                }
                if (hdType.Value == "1")
                {
                    drpLink.Items.FindByValue("1").Selected = true;
                }
            }
        }
    }
}